package com.biaoyoo.www.personapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.biaoyoo.www.personapp.R;
import com.biaoyoo.www.personapp.constant.ConstantValue;
import com.biaoyoo.www.personapp.core.AppStateTrack;
import com.biaoyoo.www.personapp.utils.SPUtils;

public class WelcomeActivity extends BaseActivity {

    private int mCount = 5;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case ConstantValue.COMEAPP:
                    boolean islogin = (boolean) SPUtils.get("islogin", false);
                    if(islogin){
                        lanchGoalActivity(MainActivity.class);
                    }else {
                        lanchGoalActivity(LoginActivity.class);
                    }
                    finish();
                    break;
                case ConstantValue.WEL_TIMECONSUME:
                    mCount --;
                    welTimeTxt.setText(String.valueOf(mCount));
                    if(mCount == 1){
                        mHandler.sendEmptyMessageDelayed(ConstantValue.COMEAPP,1000);
                    }else{
                        mHandler.sendEmptyMessageDelayed(ConstantValue.WEL_TIMECONSUME,1000);
                    }
                    break;
            }
        }
    };

    private TextView welTimeTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AppStateTrack.getInstance().setmCurentState(ConstantValue.STATE_OFFLINE);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void loadViews() {
        setContentView(R.layout.activity_welcome,R.string.wel_activity);
        welTimeTxt = (TextView) findViewById(R.id.tv_wel);
        welTimeTxt.setText("5");
    }
    @Override
    protected void initDatas() {
    }

    @Override
    protected void onStart() {
        super.onStart();
        mHandler.sendEmptyMessageDelayed(ConstantValue.WEL_TIMECONSUME,1000);
    }


    @Override
    protected void onStop() {
        super.onStop();
        if(mHandler !=null){
            mHandler.removeMessages(ConstantValue.COMEAPP);
            mHandler.removeMessages(ConstantValue.WEL_TIMECONSUME);
        }
    }
}
